package no.ciber.kartdemo.facebook.auth;

import static no.ciber.kartdemo.domain.GmlUtil.featureCollectionEnd;
import static no.ciber.kartdemo.domain.GmlUtil.featureCollectionStart;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import no.ciber.kartdemo.domain.GmlUtil;
import no.ciber.kartdemo.facebook.client.FacebookClientHelper;
import no.ciber.kartdemo.facebook.client.FacebookUserService;

import org.json.JSONObject;

import com.google.code.facebookapi.JsonHelper;
import com.google.code.facebookapi.schema.User;

public class CheckLoggedInFacebookUserServlet extends HttpServlet {

	private static final long serialVersionUID = 8749381993161016679L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		FacebookUserService service = new FacebookUserService();
		initierFacebookKlient(req);
		List<User> venneliste = service.hentVenneliste();
//		skrivUtHtml(resp, venneliste);
		skrivUtGml(resp, venneliste);
	}

	private void skrivUtGml(HttpServletResponse response, List<User> venneliste) throws IOException {
		response.setContentType("text/xml; charset=ISO-8859-1");
		response.setHeader("Content-Disposition", "attachment; filename=facebookvenner.xml");
		response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
		response.setHeader("Pragma", "public");
		ServletOutputStream output = response.getOutputStream();
		output.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
		StringBuffer gml = new StringBuffer(); 
		gml.append(featureCollectionStart());
		for (int i = 0; i < venneliste.size(); i++) {
			gml.append(GmlUtil.featureMember(venneliste.get(i), i));
		}
		gml.append(featureCollectionEnd());
		output.println(gml.toString());
	}

	private void skrivUtHtml(HttpServletResponse resp, List<User> venneliste) throws IOException {
		resp.setContentType("text/xml; charset=UTF-8");
		ServletOutputStream outputStream = resp.getOutputStream();
		outputStream.println("<html><body>");
		outputStream.println("<h1>Innloggingsinformasjon fra Facebook</h1><BR>");
		for (User user : venneliste) {
			outputStream.println("<BR>User: " + user.getFirstName() + " " + user.getLastName());
			outputStream.println(", Info: " + user.getSex());
			outputStream.println(", currentBy:" + user.getCurrentLocation().getCity());
			outputStream.println(", lat:" + user.getCurrentLocation().getLatitude());
			outputStream.println(", lonBy:" + user.getCurrentLocation().getLongitude());
			// if (user.getHometownLocation() != null) {
			// outputStream.println(" By:" + user.getHometownLocation().getCity());
			// outputStream.println(" Postnummer:" + user.getHometownLocation().getZip());
			// outputStream.println(" Adresse: " + user.getHometownLocation().getStreet());
			// }
			// if (user.getCurrentLocation() != null) {
			// outputStream.println(" currentPostnummer:" + user.getCurrentLocation().getZip());
			// outputStream.println(" currentAdresse: " + user.getCurrentLocation().getStreet());
			// }
		}

		outputStream.println("</body></html>");
	}

	private void initierFacebookKlient(HttpServletRequest req) {
		String facebookSession = req.getParameter("session");
		JSONObject session = (JSONObject) JsonHelper.jsonToJavaValue(facebookSession);
		new FacebookClientHelper(session);
	}

}
